package com.isunland.managesystem.ui;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.utils.LogUtil;

/**
 * 视频流形式播放
 *
 * @author wangyanan
 */
public class VideoActivity extends BaseVolleyActivity {
  private static final String TAG = "VideoActivity";
  public static final String EXTRA_VIDEO_URL = "com.isunland.palmstudysystem.ui.EXTRA_VIDEO_URL";
  //	public static final String EXTRA_VIDEO_CONTENT = "com.isunland.palmstudysystem.ui.EXTRA_VIDEO_CONTENT";
  private ProgressDialog progressDialog;
  //	private MediaController mediaController;
  private VideoView videoView;
  //	private TextView mTvContent;

  //考试系统视频地址
  private String url;
  //	private String mStrContent;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
    this.getWindow()
        .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置状态栏
    setContentView(R.layout.activity_media);
    url = getIntent().getStringExtra(EXTRA_VIDEO_URL);
    url = ApiConst.getAbsoluteUrl(ApiConst.URL_WEIJIA_INTRODUCTION);
    videoView = (VideoView) findViewById(R.id.videoView);
    LogUtil.i("videoUrl=" + url);
    if (!TextUtils.isEmpty(url)) {

      playvideo(url);
    } else {
      Toast.makeText(this, "无法获取视频地址", Toast.LENGTH_SHORT).show();
    }
  }

  public void playvideo(String videopath) {
    Log.e("entered", "playvide");
    Log.e("path is", "" + videopath);
    try {

      progressDialog = ProgressDialog.show(VideoActivity.this, "", "视频正在加载,请稍候...", false);

      progressDialog.setOnKeyListener(new OnKeyListener() {

        @Override public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
          switch (arg1) {
            case KeyEvent.KEYCODE_BACK:
              backPress();
              return true;
            default:
              break;
          }
          return false;
        }
      });
      progressDialog.setCanceledOnTouchOutside(false);//点击外部不取消
      getWindow().setFormat(PixelFormat.TRANSLUCENT);

      //	        mediaController = new MediaController(VideoActivity.this);
      MediaController mediaController = new MediaController(this);
      mediaController.setAnchorView(videoView);
      videoView.setMediaController(mediaController);
      Uri video = Uri.parse(videopath);
      //	        videoView.setMediaController(mediaController);
      videoView.setVideoURI(video);

      videoView.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
          progressDialog.dismiss();
          videoView.start();
        }
      });
      videoView.setOnErrorListener(new OnErrorListener() {

        @Override public boolean onError(MediaPlayer mp, int what, int extra) {
          Toast.makeText(VideoActivity.this, "播放出错", Toast.LENGTH_SHORT).show();
          VideoActivity.this.finish();
          return true;
        }
      });
    } catch (Exception e) {
      progressDialog.dismiss();
      Log.e(TAG, "Video Play Error :" + e.getMessage(), e);
    }
  }

  @Override public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode) {
      case KeyEvent.KEYCODE_BACK:
        backPress();
        return true;
      default:
        break;
    }

    return super.onKeyDown(keyCode, event);
  }

  private void backPress() {
    videoView.setOnErrorListener(null);
    VideoActivity.this.finish();
  }

  /**
   * 响应返回按钮
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(this) != null) {
          NavUtils.navigateUpFromSameTask(this);
        }
        break;

      default:
        return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
  }
}
